package com.gitee.zhanyao.test.springrestdoc.controller;

import com.gitee.zhanyao.test.springrestdoc.model.Responses;
import com.gitee.zhanyao.test.springrestdoc.model.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value = {"/api/user"}, produces = {"application/json"})
public class UserController {

    @RequestMapping(method = {RequestMethod.POST})
    @ResponseBody
    public ResponseEntity<User> createUser(User user) {
        return new ResponseEntity(user, HttpStatus.OK);
    }


    @RequestMapping(value = {"/createWithArray"}, method = {RequestMethod.POST})
    @ResponseBody
    public ResponseEntity<User> createUsersWithArrayInput(User[] users) {
        return Responses.ok();
    }


    @RequestMapping(value = {"/createWithList"}, method = {RequestMethod.POST})
    @ResponseBody
    public ResponseEntity<String> createUsersWithListInput(List<User> users) {
        return Responses.ok();
    }


    @RequestMapping(value = {"/{username}"}, method = {RequestMethod.PUT})
    @ResponseBody
    public ResponseEntity<String> updateUser(@PathVariable("username") String username, User user) {
        return Responses.notFound();
    }


    @RequestMapping(value = {"/{username}"}, method = {RequestMethod.DELETE})
    public ResponseEntity<String> deleteUser(@PathVariable("username") String username) {
        return Responses.notFound();
    }


    @RequestMapping(value = {"/{username}"}, method = {RequestMethod.GET})
    public ResponseEntity<User> getUserByName(@PathVariable("username") String username) {
        throw new NotFoundException(404, "User not found");
    }


    @RequestMapping(value = {"/login"}, method = {RequestMethod.GET})
    public ResponseEntity<String> loginUser(@RequestParam("username") String username, @RequestParam("password") String password) {
        return new ResponseEntity("logged in user session:" + System.currentTimeMillis(), HttpStatus.OK);
    }


    @RequestMapping(value = {"/logout"}, method = {RequestMethod.GET})
    public ResponseEntity<String> logoutUser() {
        return Responses.ok();
    }
}